// 初始化 store 仓库
import { defineStore } from 'pinia'
import { Names } from './store-name'
type Params = {
  userName: string,
  password: string | number
}
export const useStore = defineStore(Names.TEST, {
  // 全局状态
  state: () => {
    return {
      index: 0,
      userInfo: {
        userName: localStorage.getItem('username') ? localStorage.getItem('username') : '',
        password: '',
      }
    }
  },
  // 类似于 computed 修饰一些值，内部的方法可以相互调用
  getters: {},
  // 类似于 methods 可以做一些同步或异步的方法 提交 state，内部的方法可以相互调用
  actions: {
    login(params:Params):void {
      this.userInfo = params
      localStorage.setItem('username', params.userName)
    },
    logout():void {
      this.userInfo = {}
      localStorage.clear()
    }
  },
})